home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / udp.c < prev    next >
C/C++ Source or Header  |  1991-05-10  |  7KB  |  292 lines

  1. /* @(#) $Header: udp.c,v 1.5 91/05/09 07:39:09 deyke Exp $ */
  2.  
  3. /* Internet User Data Protocol (UDP)
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "netuser.h"
  9. #include "iface.h"
  10. #include "udp.h"
  11. #include "ip.h"
  12. #include "internet.h"
  13. #include "icmp.h"
  14.  
  15. static struct udp_cb *lookup_udp __ARGS((struct socket *socket));
  16.  
  17. struct mib_entry Udp_mib[] = {
  18.     "",                     0,
  19.     "udpInDatagrams",       0,
  20.     "udpNoPorts",           0,
  21.     "udpInErrors",          0,
  22.     "udpOutDatagrams",      0,
  23. };
  24.  
  25. /* UDP control structures list */
  26. struct udp_cb *Udps;
  27.  
  28. /* Create a UDP control block for lsocket, so that we can queue
  29.  * incoming datagrams.
  30.  */
  31. struct udp_cb *
  32. open_udp(lsocket,r_upcall)
  33. struct socket *lsocket;
  34. void (*r_upcall)();
  35. {
  36.     register struct udp_cb *up;
  37.  
  38.     if((up = lookup_udp(lsocket)) != NULLUDP){
  39.         /* Already exists */
  40.         Net_error = CON_EXISTS;
  41.         return NULLUDP;
  42.     }
  43.     up = (struct udp_cb *)callocw(1,sizeof (struct udp_cb));
  44.     up->socket.address = lsocket->address;
  45.     up->socket.port = lsocket->port;
  46.     up->r_upcall = r_upcall;
  47.  
  48.     up->next = Udps;
  49.     Udps = up;
  50.     return up;
  51. }
  52.  
  53. /* Send a UDP datagram */
  54. int
  55. send_udp(lsocket,fsocket,tos,ttl,data,length,id,df)
  56. struct socket *lsocket;         /* Source socket */
  57. struct socket *fsocket;         /* Destination socket */
  58. char tos;                       /* Type-of-service for IP */
  59. char ttl;                       /* Time-to-live for IP */
  60. struct mbuf *data;              /* Data field, if any */
  61. int16 length;                   /* Length of data field */
  62. int16 id;                       /* Optional ID field for IP */
  63. char df;                        /* Don't Fragment flag for IP */
  64. {
  65.     struct mbuf *bp;
  66.     struct pseudo_header ph;
  67.     struct udp udp;
  68.     int32 laddr;
  69.  
  70.     if(length != 0 && data != NULLBUF)
  71.         trim_mbuf(&data,length);
  72.     else
  73.         length = len_p(data);
  74.  
  75.     length += UDPHDR;
  76.  
  77.     laddr = lsocket->address;
  78.     if(laddr == INADDR_ANY)
  79.         laddr = locaddr(fsocket->address);
  80.  
  81.     udp.source = lsocket->port;
  82.     udp.dest = fsocket->port;
  83.     udp.length = length;
  84.  
  85.     /* Create IP pseudo-header, compute checksum and send it */
  86.     ph.length = length;
  87.     ph.source = laddr;
  88.     ph.dest = fsocket->address;
  89.     ph.protocol = UDP_PTCL;
  90.  
  91.     if((bp = htonudp(&udp,data,&ph)) == NULLBUF){
  92.         Net_error = NO_MEM;
  93.         free_p(data);
  94.         return 0;
  95.     }
  96.     udpOutDatagrams++;
  97.     ip_send(laddr,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
  98.     return (int)length;
  99. }
  100. /* Accept a waiting datagram, if available. Returns length of datagram */
  101. int
  102. recv_udp(up,fsocket,bp)
  103. register struct udp_cb *up;
  104. struct socket *fsocket;         /* Place to stash incoming socket */
  105. struct mbuf **bp;               /* Place to stash data packet */
  106. {
  107.     struct socket sp;
  108.     struct mbuf *buf;
  109.     int16 length;
  110.  
  111.     if(up == NULLUDP){
  112.         Net_error = NO_CONN;
  113.         return -1;
  114.     }
  115.     if(up->rcvcnt == 0){
  116.         Net_error = WOULDBLK;
  117.         return -1;
  118.     }
  119.     buf = dequeue(&up->rcvq);
  120.     up->rcvcnt--;
  121.  
  122.     /* Strip socket header */
  123.     pullup(&buf,(char *)&sp,sizeof(struct socket));
  124.  
  125.     /* Fill in the user's foreign socket structure, if given */
  126.     if(fsocket != NULLSOCK){
  127.         fsocket->address = sp.address;
  128.         fsocket->port = sp.port;
  129.     }
  130.     /* Hand data to user */
  131.     length = len_p(buf);
  132.     if(bp != NULLBUFP)
  133.         *bp = buf;
  134.     else
  135.         free_p(buf);
  136.     return (int)length;
  137. }
  138. /* Delete a UDP control block */
  139. int
  140. del_udp(conn)
  141. struct udp_cb *conn;
  142. {
  143.     struct mbuf *bp;
  144.     register struct udp_cb *up;
  145.     struct udp_cb *udplast = NULLUDP;
  146.  
  147.     for(up = Udps;up != NULLUDP;udplast = up,up = up->next){
  148.         if(up == conn)
  149.             break;
  150.     }
  151.     if(up == NULLUDP){
  152.         /* Either conn was NULL or not found on list */
  153.         Net_error = INVALID;
  154.         return -1;
  155.     }
  156.     /* Get rid of any pending packets */
  157.     while(up->rcvcnt != 0){
  158.         bp = up->rcvq;
  159.         up->rcvq = up->rcvq->anext;
  160.         free_p(bp);
  161.         up->rcvcnt--;
  162.     }
  163.     /* Remove from list */
  164.     if(udplast != NULLUDP)
  165.         udplast->next = up->next;
  166.     else
  167.         Udps = up->next;        /* was first on list */
  168.  
  169.     free((char *)up);
  170.     return 0;
  171. }
  172. /* Process an incoming UDP datagram */
  173. void
  174. udp_input(iface,ip,bp,rxbroadcast)
  175. struct iface *iface;    /* Input interface */
  176. struct ip *ip;          /* IP header */
  177. struct mbuf *bp;        /* UDP header and data */
  178. int rxbroadcast;        /* The only protocol that accepts 'em */
  179. {
  180.     struct pseudo_header ph;
  181.     struct udp udp;
  182.     struct udp_cb *up;
  183.     struct socket lsocket;
  184.     struct socket fsocket;
  185.     struct mbuf *packet;
  186.     int16 length;
  187.  
  188.     if(bp == NULLBUF)
  189.         return;
  190.  
  191.     /* Create pseudo-header and verify checksum */
  192.     ph.source = ip->source;
  193.     ph.dest = ip->dest;
  194.     ph.protocol = ip->protocol;
  195.     length = ip->length - IPLEN - ip->optlen;
  196.     ph.length = length;
  197.  
  198.     /* Peek at header checksum before we extract the header. This
  199.      * allows us to bypass cksum() if the checksum field was not
  200.      * set by the sender.
  201.      */
  202.     udp.checksum = udpcksum(bp);
  203.     if(udp.checksum != 0 && cksum(&ph,bp,length) != 0){
  204.         /* Checksum non-zero, and wrong */
  205.         udpInErrors++;
  206.         free_p(bp);
  207.         return;
  208.     }
  209.     /* Extract UDP header in host order */
  210.     if(ntohudp(&udp,&bp) != 0){
  211.         /* Truncated header */
  212.         udpInErrors++;
  213.         free_p(bp);
  214.         return;
  215.     }
  216.     /* If this was a broadcast packet, pretend it was sent to us */
  217.     if(rxbroadcast){
  218.         lsocket.address = iface->addr;
  219.     } else
  220.         lsocket.address = ip->dest;
  221.  
  222.     lsocket.port = udp.dest;
  223.     /* See if there's somebody around to read it */
  224.     if((up = lookup_udp(&lsocket)) == NULLUDP){
  225.         /* Nope, return an ICMP message */
  226.         if(!rxbroadcast){
  227.             bp = htonudp(&udp,bp,&ph);
  228.             icmp_output(ip,bp,ICMP_DEST_UNREACH,ICMP_PORT_UNREACH,NULL);
  229.         }
  230.         udpNoPorts++;
  231.         free_p(bp);
  232.         return;
  233.     }
  234.     /* Create space for the foreign socket info */
  235.     if((packet = pushdown(bp,sizeof(fsocket))) == NULLBUF){
  236.         /* No space, drop whole packet */
  237.         free_p(bp);
  238.         udpInErrors++;
  239.         return;
  240.     }
  241.     fsocket.address = ip->source;
  242.     fsocket.port = udp.source;
  243.     memcpy(&packet->data[0],(char *)&fsocket,sizeof(fsocket));
  244.  
  245.     /* Queue it */
  246.     enqueue(&up->rcvq,packet);
  247.     up->rcvcnt++;
  248.     udpInDatagrams++;
  249.     if(up->r_upcall)
  250.         (*up->r_upcall)(iface,up,up->rcvcnt);
  251. }
  252. /* Look up UDP socket.
  253.  * Return control block pointer or NULLUDP if nonexistant
  254.  * As side effect, move control block to top of list to speed future
  255.  * searches.
  256.  */
  257. static struct udp_cb *
  258. lookup_udp(socket)
  259. struct socket *socket;
  260. {
  261.     register struct udp_cb *up;
  262.     struct udp_cb *uplast = NULLUDP;
  263.  
  264.     for(up = Udps;up != NULLUDP;uplast = up,up = up->next){
  265.         if(socket->port == up->socket.port
  266.          && (socket->address == up->socket.address
  267.          || up->socket.address == INADDR_ANY)){
  268.             if(uplast != NULLUDP){
  269.                 /* Move to top of list */
  270.                 uplast->next = up->next;
  271.                 up->next = Udps;
  272.                 Udps = up;
  273.             }
  274.             return up;
  275.         }
  276.     }
  277.     return NULLUDP;
  278. }
  279.  
  280. /* Attempt to reclaim unused space in UDP receive queues */
  281. void
  282. udp_garbage(red)
  283. int red;
  284. {
  285.     register struct udp_cb *udp;
  286.  
  287.     for(udp = Udps;udp != NULLUDP; udp = udp->next){
  288.         mbuf_crunch(&udp->rcvq);
  289.     }
  290. }
  291.  
  292.